 <template>
   <el-card shadow="always" class="box-card">
      <div class="card-name search-item">
        <div class="name width2">代理商列表</div>
        <div class="mr-30"><el-button type="primary" @click="add">新 增</el-button></div>
      </div>
      <div class="content-con">
        <el-form :model="form" ref="form" size="medium" label-width="100px" class="bank-search">
          <el-form-item label="代理商ID:" prop="agent_id">
            <el-input v-model="form.agent_id" placeholder="代理商ID"></el-input>
          </el-form-item>
          <el-form-item label="代理商级别:" prop="agent_level">
            <el-select v-model="form.agent_level" filterable clearable placeholder="代理商级别" style="width: 100%;">
              <el-option
                v-for="item in levelList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-button class="save mb-22 ml-20" @click="search">查 询</el-button>
        </el-form>

        <div class="bank-form">
          <div class="form-item">
            <el-table
              :data="tableData.slice((currentPage-1)*pagesize,currentPage*pagesize)"
              class="table"
              stripe border
              :header-cell-style="{background:'#f3f3f3',textAlign: 'center'}"
              :cell-style="{ textAlign: 'center' }"><!--:data="用于存放请求数据回来的数组" -->
              <el-table-column prop="agent_id" label="代理商ID" width="160">
              </el-table-column>
              <el-table-column prop="agent" label="代理商名称" width="140">
              </el-table-column>
              <el-table-column prop="agent_level" label="代理商级别" width="140">
                <template slot-scope="scope">
                  <span v-for="item in levelList" :key="item.value" :value="item.value">
                    <span v-if="item.value == scope.row.agent_level">{{item.label}}</span>
                  </span>
                </template>
              </el-table-column>
              <el-table-column prop="uper" label="所属上级" width="160">
              </el-table-column>
              <el-table-column prop="lower" label="所属下级" width="160">
              </el-table-column>
              <el-table-column prop="address" label="联系地址" width="260">
              </el-table-column>
              <el-table-column prop="phone" label="电话" width="140">
              </el-table-column>
              <el-table-column prop="status" label="状态" width="160">
                <template slot-scope="scope">
                  <el-tag v-if="scope.row.status == 0" type="success">启用</el-tag>
                  <el-tag v-else type="success">关闭</el-tag>
                </template>
              </el-table-column>
              <el-table-column label="操作" width="240">
                <template slot-scope="scope">
                  <el-button type="text" @click="handleEdit(scope.row)">
                    <i class="el-icon-edit"></i>编辑
                  </el-button>
                  <el-button type="text" @click.native.prevent="deleteRow(scope.$index,tableData)">
                    <i class="el-icon-delete"></i>删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <!-- 分页-页码 -->
          <div class="page">
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-sizes="[5, 10, 15, 20]"
              :page-size="pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="tableData.length">
            </el-pagination>
          </div>
        </div>
      </div>
      <agent-edit ref="AgentEdit" :rows="rows"></agent-edit>
   </el-card>

 </template>
<script>
import AgentEdit from '@/components/agent-manage/AgentEdit';
export default {
  components: {
    AgentEdit
  },
  data () {
    return {
      rows: '',
      currentPage: 1, //当前页码
      pagesize: 5,    //每页的数据条数
      tableData: [
        {
          agent_id: '001',
          agent: '代理商1号',
          agent_level: '1',
          uper: '--',
          lower: '测试门店',
          address: '上海市广中路88号8楼',
          phone: '13810187109',
          status: 0
        },
        {
          agent_id: '002',
          agent: '代理商2号',
          agent_level: '2',
          uper: '--',
          lower: '小明',
          address: '广西壮族自治区钦州市钦南区3333号',
          phone: '13810187109',
          status: 0
        }
      ],
      form: {
        agent_id: '',
        agent_level: ''
      },
      levelList: [
        {value: '0',label: '总代理'},
        {value: '1',label: '一级代理'},
        {value: '2',label: '二级代理'}
      ]
    }
  },

  methods: {
    //搜索
    search() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          console.log(this.form)
          this.$message.success('成功!');
        } else {
          console.log('error submit!!');
          this.$message.error('失败!');
          return false;
        }
      });
    },

    // 增加
    add(){
      this.$nextTick(()=>{
        this.$refs.AgentEdit.openAdd()
      });
    },

    //编辑操作
    handleEdit(row){
      this.rows = row;
      this.$nextTick(()=>{
        this.$refs.AgentEdit.openEdit()
      });
    },

    //删除操作
    deleteRow(index, rows){
      rows.splice(index, 1);
    },

    //每页条数改变时触发事件,选择一页显示多少行
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.currentPage = 1;
      this.pagesize = val;
    },
    // 当前页改变时触发 跳转其他页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
    }
  }
}
</script>
<style lang="scss">

</style>
